<?php

class AdminAction extends PublicAction
{
    protected $adminModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->adminModel = D('Admin');
    }

    //用户列表
    public function index()
    {
        $map = array();

        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%' . $keyword . '%');
        $map['id|username'] = array($condition, $condition, '_multi' => true); //搜索条件

        $roleid = I('roleid', 0, 'intval');
        if ($roleid) {
            $map['role'] = $roleid;
        }

        $count = $this->adminModel->where($map)->count();

        import('@.ORG.Page');
        $page = new Page($count, 15);
        $show = $page->show();
        $list = $this->adminModel->where($map)->order('id ASC')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);// 赋值分页输出

        $role = M('AuthGroup')->getField('id,name');
        $this->assign('role',$role);

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->display();
    }

    // 添加用户
    public function add()
    {
        if (IS_POST) {

            if ($_POST['password'] != $_POST['repassword']) {
                $this->error('两次输入密码不一致！');
            }
            $_POST['salt'] = genRandomString(6);
            $_POST['password'] = md5($_POST['password'] . md5($_POST['salt']));

            //根据表单提交的POST数据创建数据对象
            if ($this->adminModel->create()) {
                $user_id = $this->adminModel->add();
                if($user_id){
                    $data['uid'] = $user_id;
                    $data['group_id'] = $_POST['group_id'];
                    if (M("AuthGroupAccess")->data($data)->add()){
                        $this->success('添加成功！', cookie('__forward__'));
                    }else{
                        $this->error('用户添加成功,但角色对应关系添加失败!');
                    }
                }else{
                    $this->error('添加失败!');
                }
            } else {
                $this->error($this->adminModel->getError());
            }
        } else {
            $groups = M('AuthGroup')->select();
            $this->assign('groups',$groups);
            $this->display();
        }
    }

    // 编辑用户
    public function edit()
    {
        if (IS_POST) {
            $password = $_POST['password'];
            $repassword = $_POST['repassword'];

            //为空 不修改密码
            if (empty($password) && empty($repassword)) {
                unset($_POST['password']);
                unset($_POST['repassword']);
            }

            if (!empty($password) || !empty($repassword)) {
                if ($password != $repassword) {
                    $this->error('两次输入密码不一致！');
                }
                $_POST['salt'] = genRandomString(6);
                $_POST['password'] = md5($password . md5($_POST['salt']));
            }

            //根据表单提交的POST数据创建数据对象
            if ($this->adminModel->create()) {
                if ($this->adminModel->save()) {
                    $where['uid'] = $_POST['id'];
                    $data['group_id'] = $_POST['group_id'];
                    M("AuthGroupAccess")->where($where)->save($data);
                    $this->success('编辑成功！', cookie('__forward__'));
                }
            } else {
                $this->error($this->adminModel->getError());
            }
        } else {
            $id = I('get.id');
            $info = $this->adminModel->getUser(array('id' => $id));
            $this->assign('info', $info);

            $group_id = M('AuthGroupAccess')->where('uid='.$id)->getField('group_id');
            $this->assign('group_id',$group_id);

            $groups = M('AuthGroup')->select();
            $this->assign('groups',$groups);

            $this->display();
        }
    }

    //删除用户
    public function delete()
    {
        $id = I('id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误!');
        } else if ($id == 1) {     //无视系统权限的那个用户不能删除
            $this->error('禁止删除此用户!');
        }

        if ($this->adminModel->delUser('id=' . $id)) {
            if(M("AuthGroupAccess")->where('uid='.$id)->delete()){
                $this->success('删除成功！');
            }else{
                $this->error('用户成功,但角色对应关系删除失败!');
            }
        } else {
            $this->error('删除失败!');
        }
    }
}